package main

import (
	"encoding/json"
	"fmt"
	"time"
)

/*
定义返回的数据格式，将返回的数据使用json的方式打印出来
// Code,Message,Data
// s.json() =>转化成json格式
标准化返回
{
	"code": 1001
	"message": "参数不完整"
	"data": {}
}
*/
type ApiInterface interface {
	apiJsonSuccess()
	apiJsonFailure()
}
type Api struct {
	Code int `json:"code"`
	Message string `json:"message"`
	Data map[string]string `json:"data"`
}
func (a Api) apiJsonSuccess(){
	currentTime := time.Now()
	timeString := currentTime.Format("2006/01/02 15:04:05")
	api := Api{
		Code: 1000,
		Message: "访问成功",
		Data: map[string]string{ "date":timeString,"email":"123@sc.com","address":"湖南长沙"},
	}
	mData,err := json.Marshal(api)
	if err != nil {
	}else {
		fmt.Println(string(mData))
	}
}
func (a Api) apiJsonFailure(){
	api := Api{
		Code: 5000,
		Message: "访问失败",
		Data: map[string]string{ "date":"","email":"","address":""},
	}
	mData,err := json.Marshal(api)
	if err != nil {
	}else {
		fmt.Println(string(mData))
	}
}
func jsons(a ApiInterface,input string)  {
	if input == "1"{
		a.apiJsonSuccess()
	}else {
		a.apiJsonFailure()
	}
}
func main()  {
	fmt.Println("模拟接口标准化返回，1--返回成功/2--返回失败:")
	var input string
	fmt.Scan(&input)
	api := Api{}
	jsons(api, string(input))
}
